<?php


namespace app\cms\admin\controller\saas;

use app\cms\authCallBack\saas;
use app\cms\model\saas\saasClientServerModel;
use iflow\auth\Authorize\AuthAnnotation;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\{ GetMapping, PostMapping, DeleteMapping };

#[Controller('%cmsAdmin%/saas_server')]
class saasServerController
{
    /**
     * 获取saas服务器列表
     * @param string $server_name
     * @param string $server_ip
     * @return Response
     */
    #[GetMapping]
    #[AuthAnnotation('saasAdmin', 'saasServerRead|saasServerReadWrite', saas::class . '@validIsSaasAccountValidServer')]
    public function getSaasServerList(
        string $server_name = '',
        string $server_ip = ''
    ): Response {
        return (new saasClientServerModel()) -> getSaasClientServerList(
            $server_name, $server_ip
        );
    }

    /**
     * 添加Saas服务
     * @param string $server_name
     * @param string $server_ip
     * @param int $server_port
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('saasAdmin', 'saasServerWrite|saasServerReadWrite', saas::class . '@validIsSaasAccountValidServer')]
    public function addSaasServer(
        string $server_name, string $server_ip, int $server_port
    ): Response {
        try {
            validator([
                'server_name' => 'required|min:1',
                'server_ip' => 'require|ip',
                'server_port' => 'require|number|min:1024'
            ], [
                'server_name' => $server_name,
                'server_ip' => $server_ip,
                'server_port' => $server_port,
            ], [
                'server_name.required' => 'Saas服务器名称不能为空',
                'server_name.min' => 'Saas服务器名称长度最新为1',
                'server_ip.require' => 'Saas服务器IP不能为空',
                'server_ip.ip' => 'Saas服务器IP格式错误',
                'server_port.require' => 'Saas服务器端口不能为空',
                'server_port.number' => 'Saas服务器端口格式错误',
            ]);
            return (new saasClientServerModel()) -> addSaasServer($server_name, $server_ip, $server_port, 1);
        } catch (\Exception $exception) {
            return message() -> parameter_error($exception -> getMessage());
        }
    }

    /**
     * 更新Saas服务器
     * @param int $server_id
     * @param string $server_name
     * @param string $server_ip
     * @param int $server_port
     * @param int $status
     * @return Response
     */
    #[PostMapping]
    #[AuthAnnotation('saasAdmin', 'saasServerWrite|saasServerReadWrite', saas::class . '@validIsSaasAccountValidServer')]
    public function updateSaasServer(
        int $server_id, string $server_name, string $server_ip, int $server_port, int $status = -1
    ): Response {
        try {
            validator([
                'server_id' => 'required|number',
                'server_name' => 'required|min:1',
                'server_ip' => 'require|ip',
                'server_port' => 'require|number|min:1024',
            ], [
                'server_name' => $server_name,
                'server_ip' => $server_ip,
                'server_port' => $server_port
            ], [
                'server_id.required' => '请选择需要更新Saas服务器',
                'server_id.number' => '请选择需要更新Saas服务器',
                'server_name.required' => 'Saas服务器名称不能为空',
                'server_name.min' => 'Saas服务器名称长度最新为1',
                'server_ip.require' => 'Saas服务器IP不能为空',
                'server_ip.ip' => 'Saas服务器IP格式错误'
            ]);
            return (new saasClientServerModel()) -> updateSaasServer($server_id, $server_name, $server_ip, $status);
        } catch (\Exception $exception) {
            return message() -> parameter_error($exception -> getMessage());
        }
    }

    /**
     * 删除Saas节点服务器
     * @param int $server_id
     * @return Response
     */
    #[DeleteMapping('delete_saas_server/<[0-9]{1,}:server_id>')]
    #[AuthAnnotation('saasAdmin', 'saasServerWrite|saasServerReadWrite', saas::class . '@validIsSaasAccountValidServer')]
    public function deleteSaasServer(int $server_id): Response
    {
        return (new saasClientServerModel()) -> deleteData([
            'server_id' => $server_id
        ]);
    }

}